﻿Imports DAL

Public Class FrmRecurrence
#Region "properties"
    Private DefaultDate As Date = New Date(2000, 1, 1, 0, 0, 0)
    Private ProgValue As Boolean = False
    Private isloading As Boolean

    Public Property Titre() As String
        Get
            Return Tb_Titre.Text
        End Get
        Set(ByVal value As String)
            Tb_Titre.Text = value
        End Set
    End Property

    Public Property Description() As String
        Get
            Return Tb_Description.Text
        End Get
        Set(ByVal value As String)
            Tb_Description.Text = value
        End Set
    End Property

    Private _Accompagnement As Boolean = False
    Public Property Accompagnement() As Boolean
        Get
            Return _Accompagnement
        End Get
        Set(value As Boolean)
            _Accompagnement = value
        End Set
    End Property


    Public Property DateDebut() As TimeSpan
        Get
            Return New TimeSpan(Dtp_Debut.Value.TimeOfDay.Ticks)
        End Get
        Set(ByVal value As TimeSpan)
            Dtp_Debut.Value = DefaultDate.AddTicks(value.Ticks)
        End Set
    End Property

    Public Property Duree() As TimeSpan
        Get
            Return New TimeSpan(Dtp_Duree.Value.TimeOfDay.Ticks)
        End Get
        Set(ByVal value As TimeSpan)
            Dtp_Duree.Value = DefaultDate.AddTicks(value.Ticks)
        End Set
    End Property

    Public Property Couleur() As Color
        Get
            Return Pb_CouleurPlage.BackColor
        End Get
        Set(ByVal value As Color)
            Pb_CouleurPlage.BackColor = value
        End Set
    End Property
    Enum TypeEdition
        AjoutRecurrence
        ModificationRecurrence
        LectureRecurrence
    End Enum

    Private _modeEdition As TypeEdition = TypeEdition.AjoutRecurrence
    Public Property ModeEdition() As TypeEdition
        Get
            Return _modeEdition
        End Get
        Set(ByVal value As TypeEdition)
            _modeEdition = value

            If value = TypeEdition.LectureRecurrence Then
                ToggleGeneralEdit(False)
            Else
                ToggleGeneralEdit(True)
            End If
        End Set
    End Property
    Private _ValueModified As Boolean = False
    Public ReadOnly Property ValueModified() As Boolean
        Get
            Return _ValueModified
        End Get
    End Property

    Private _Beneficiaire As String = String.Empty
    Public Property Beneficiaire() As String
        Get
            Return _Beneficiaire
        End Get
        Set(ByVal value As String)
            _Beneficiaire = value
        End Set
    End Property

    Private DebutPrecedent As Date

    Private _EmployeRecurrence As Employe
    Public Property EmployeRecurrence() As Employe
        Get
            Return _EmployeRecurrence
        End Get
        Set(ByVal value As Employe)
            _EmployeRecurrence = value
        End Set
    End Property

    Public Property Jour() As Integer
        Get
            Return CboJourRecurrence.SelectedIndex
        End Get
        Set(ByVal value As Integer)
            CboJourRecurrence.SelectedIndex = value
        End Set
    End Property

#End Region


    Private Sub DTPDebut_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Dtp_Debut.ValueChanged
        If Not Me.DesignMode Then
            If Not IsLoading Then
                IsLoading = True
                If Not Dtp_Debut.Value.TimeOfDay = DefaultDate.AddMinutes(1439).TimeOfDay Then
                    Dim newFin = Dtp_Debut.Value.AddTicks(Dtp_Duree.Value.TimeOfDay.Ticks)
                    If newFin.TimeOfDay <= Dtp_Debut.Value.TimeOfDay Then
                        Dtp_Fin.Value = DefaultDate.AddMinutes(1439)
                        Dtp_Duree.Value = DefaultDate.AddTicks((Dtp_Fin.Value.TimeOfDay - Dtp_Debut.Value.TimeOfDay).Ticks)
                    Else
                        Dtp_Fin.Value = newFin
                    End If
                Else
                    Dtp_Debut.Value = DebutPrecedent
                End If
                IsLoading = False
            End If
            DebutPrecedent = Dtp_Debut.Value
        End If
    End Sub
    Private Sub DTPDuree_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Dtp_Duree.ValueChanged
        If Not Me.DesignMode Then
            If Not IsLoading Then
                IsLoading = True
                Dim newFin = Dtp_Debut.Value.AddTicks(Dtp_Duree.Value.TimeOfDay.Ticks)
                If newFin.TimeOfDay <= Dtp_Debut.Value.TimeOfDay Then
                    Dtp_Fin.Value = DefaultDate.AddMinutes(1439)
                    Dtp_Duree.Value = DefaultDate.AddTicks((Dtp_Fin.Value.TimeOfDay - Dtp_Debut.Value.TimeOfDay).Ticks)
                Else
                    Dtp_Fin.Value = newFin
                End If
                IsLoading = False
            End If
        End If
    End Sub
    Private Sub DTPFin_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Dtp_Fin.ValueChanged
        If Not Me.DesignMode Then
            If Not IsLoading Then
                IsLoading = True
                If Dtp_Fin.Value.TimeOfDay <= Dtp_Debut.Value.TimeOfDay Then
                    Dtp_Fin.Value = Dtp_Debut.Value.AddMinutes(1)
                Else
                    Dtp_Duree.Value = DefaultDate.AddTicks((Dtp_Fin.Value.TimeOfDay - Dtp_Debut.Value.TimeOfDay).Ticks)
                End If
                IsLoading = False
            End If
        End If
    End Sub

    Private Sub FrmRecurrence_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        DefaultDate = New Date(2000, 1, 1, 0, 0, 0)
        'Dtp_Debut.Value = DefaultDate
        Dtp_Debut.MinDate = DefaultDate
        Dtp_Debut.MaxDate = DefaultDate.AddHours(24)

        Dtp_Fin.MinDate = DefaultDate
        Dtp_Fin.MaxDate = DefaultDate.AddHours(24)
        Me.Height = 100
        CboJourRecurrence.SelectedIndex = 0
        If Not _EmployeRecurrence Is Nothing Then
            TbEmploye.Text = _EmployeRecurrence.Nom + ", " + _EmployeRecurrence.Prenom
        End If
        'Dtp_Fin.Value = DefaultDate.AddHours(2)
    End Sub

    Private Sub Btn_SelectEmp_Click(sender As System.Object, e As System.EventArgs) Handles Btn_SelectEmp.Click
        Using diag As New FrmEmployePicker
            Using dbc As New BDCarpeDiemDataContext
                If Not _EmployeRecurrence Is Nothing Then
                    diag.SelectedEmployes = (From x In dbc.Employes Where (x.Supprime = False) _
                                AndAlso x.ID = _EmployeRecurrence.ID Select x).ToList()
                End If
                
                diag.SingleSelection = True
                If diag.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    _EmployeRecurrence = diag.SelectedEmployes(0)
                    TbEmploye.Text = _EmployeRecurrence.Nom + ", " + _EmployeRecurrence.Prenom
                End If
            End Using
            
        End Using
    End Sub

    Private Sub ToggleGeneralEdit(ByVal state As Boolean)
        Tb_Titre.Enabled = state
        Tb_Description.Enabled = state
        Dtp_Duree.Enabled = state
        Dtp_Debut.Enabled = state
        Dtp_Fin.Enabled = state
        Btn_ColorPicker.Enabled = state
        TableLayoutPanel1.Visible = state
    End Sub

    Private Sub btnAccepter_Click(sender As System.Object, e As System.EventArgs) Handles btnAccepter.Click
        Dim erreur As Boolean = False
        ErrorProvider1.Clear()
        If Titre.Length < 1 Or Titre.Length > 50 Then
            ErrorProvider1.SetError(Tb_Titre, "Veuillez entrer un titre entre 1 et 50 caractère.")
            erreur = True
        End If
        If Duree.TotalMinutes < 1 Then
            ErrorProvider1.SetError(Dtp_Duree, "Veuillez entrer une durée de plus d'une minute.")
            erreur = True
        End If
        If TbEmploye.Text = String.Empty Or _EmployeRecurrence Is Nothing Then
            ErrorProvider1.SetError(TbEmploye, "Veuillez choisir un employé.")
            erreur = True
        End If
        If Not erreur Then
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If

    End Sub

    Private Sub Btn_ColorPicker_Click(sender As System.Object, e As System.EventArgs) Handles Btn_ColorPicker.Click
        Using diag As New FrmColorPicker()
            diag.Value = Pb_CouleurPlage.BackColor
            If diag.ShowDialog = DialogResult.OK Then
                Pb_CouleurPlage.BackColor = diag.Value
                If ModeEdition = TypeEdition.ModificationRecurrence AndAlso Not ValueModified Then
                    _ValueModified = True
                End If
            End If
        End Using
    End Sub

    Private Sub Form_Item_ValueChanged(sender As System.Object, e As System.EventArgs) _
        Handles Tb_Titre.TextChanged, Tb_Description.TextChanged, Dtp_Debut.ValueChanged, Dtp_Duree.ValueChanged, Dtp_Fin.ValueChanged
        If ModeEdition = TypeEdition.ModificationRecurrence AndAlso Not ValueModified Then
            _ValueModified = True
        End If
    End Sub
End Class